
/**************************************************************************************************
Title: Tropical Economics, AER P&P replication file
Authors: Solomon Hsiang and Kyle Meng
Date: 1/10/2015
Note: Code requires several user-defined STATA programs. 
1) ols_spatial_HAC: regression code that allows for spatial and serial correlation of arbitrary form. ado file available in download folder.
2) lincomest: available by typing in command line: ssc install lincomest
3) outreg2: available by typing in command line: ssc install outreg2

***************************************************************************************************/

clear
set more off



/***************************** Main Text Table 1 and Online Appendix Table 2 ******************************/
//load data
use Hsiang_Meng_AER_PP_repData, clear
xtset cID year

local tableName "table_main"
capture rm "`tableName'.xls"
capture rm "`tableName'.txt"

local d=2000
local l=5
local eList "nino34_late L_nino34_late"
local eListAdd "nino34_late+L_nino34_late"

local rList "TE WA"
foreach region in `rList'{
    
    local oList "temp_annual_crop precip_annual_crop l_yield l_prod l_agVA"
    foreach oVar in `oList' {
    
        qui ols_spatial_HAC  `oVar' `eList' _1cIDX* _1cID_* if enso_region=="`region'", ///
        lat(y_c) lon(x_c) t(year) p(cID) dist(`d') lag(`l') bartlett disp dropvar
        unique cID if e(sample)
        local cNum=r(sum)
        qui sum year if e(sample)
        local yMin=r(min)
        local yMax=r(max)
        outreg2 using "`tableName'.xls", br noni asterisk(se) dec(3) nocons nor2 keep(`eList') ctitle("`region'","`oVar'")  /// 
        addtext("Number of countries", "`cNum'", "Period", "`yMin'-`yMax'")
        lincomest `eListAdd'
        outreg2 using "`tableName'.xls", br noni asterisk(se) dec(3) nocons nor2 ctitle("`region'","`oVar'") 
    } 			
}
capture rm "`tableName'.txt"


/***************************** Online Appendix Table 3: Standard errors **********************************/
//load data
use Hsiang_Meng_AER_PP_repData, clear
xtset cID year

local tableName "table_stdErr"
capture rm "`tableName'.xls"
capture rm "`tableName'.txt"

local eList "nino34_late L_nino34_late"
local eListAdd "nino34_late+L_nino34_late"

local rList "TE WA"
foreach region in `rList'{
    
    local oList "temp_annual_crop precip_annual_crop l_yield l_prod l_agVA"
    foreach oVar in `oList' {
    
        //Spatial HAC
        local d=2000
        local l=5
        qui ols_spatial_HAC  `oVar' `eList' _1cIDX* _1cID_* if enso_region=="`region'", ///
        lat(y_c) lon(x_c) t(year) p(cID) dist(`d') lag(`l') bartlett disp dropvar
        unique cID if e(sample)
        local cNum=r(sum)
        qui sum year if e(sample)
        local yMin=r(min)
        local yMax=r(max)
        outreg2 using "`tableName'.xls", br noni dec(3) asterisk(se) nocons nor2 keep(`eList') ctitle("`region'","`oVar'")  /// 
        addtext("Number of countries", "`cNum'", "Period", "`yMin'-`yMax'", ///
        "Standard errors", "Spatial HAC", "_", "Dist=`d' Year=`l'")
        lincomest `eListAdd'
        outreg2 using "`tableName'.xls", br noni dec(3) asterisk(se) nocons nor2 ctitle("`region'","`oVar'")
        
        local d=2000
        local l=10
        qui ols_spatial_HAC  `oVar' `eList' _1cIDX* _1cID_* if enso_region=="`region'", ///
        lat(y_c) lon(x_c) t(year) p(cID) dist(`d') lag(`l') bartlett disp dropvar
        unique cID if e(sample)
        local cNum=r(sum)
        qui sum year if e(sample)
        local yMin=r(min)
        local yMax=r(max)
        outreg2 using "`tableName'.xls", br noni dec(3) asterisk(se) nocons nor2 keep(`eList') ctitle("`region'","`oVar'")  /// 
        addtext("Number of countries", "`cNum'", "Period", "`yMin'-`yMax'", ///
        "Standard errors", "Spatial HAC", "_", "Dist=`d' Year=`l'")
        lincomest `eListAdd'
        outreg2 using "`tableName'.xls", br noni dec(3) asterisk(se) nocons nor2 ctitle("`region'","`oVar'")   

        //Spatial HAC
        local d=4000
        local l=5
        qui ols_spatial_HAC  `oVar' `eList' _1cIDX* _1cID_* if enso_region=="`region'", ///
        lat(y_c) lon(x_c) t(year) p(cID) dist(`d') lag(`l') bartlett disp dropvar
        unique cID if e(sample)
        local cNum=r(sum)
        qui sum year if e(sample)
        local yMin=r(min)
        local yMax=r(max)
        outreg2 using "`tableName'.xls", br noni dec(3) asterisk(se) nocons nor2 keep(`eList') ctitle("`region'","`oVar'")  /// 
        addtext("Number of countries", "`cNum'", "Period", "`yMin'-`yMax'", ///
        "Standard errors", "Spatial HAC", "_", "Dist=`d' Year=`l'")
        lincomest `eListAdd'
        outreg2 using "`tableName'.xls", br noni dec(3) asterisk(se) nocons nor2 ctitle("`region'","`oVar'") 
     
        //Spatial HAC
        local d=4000
        local l=10
        qui ols_spatial_HAC  `oVar' `eList' _1cIDX* _1cID_* if enso_region=="`region'", ///
        lat(y_c) lon(x_c) t(year) p(cID) dist(`d') lag(`l') bartlett disp dropvar
        unique cID if e(sample)
        local cNum=r(sum)
        qui sum year if e(sample)
        local yMin=r(min)
        local yMax=r(max)
        outreg2 using "`tableName'.xls", br noni dec(3) asterisk(se) nocons nor2 keep(`eList') ctitle("`region'","`oVar'")  /// 
        addtext("Number of countries", "`cNum'", "Period", "`yMin'-`yMax'", ///
        "Standard errors", "Spatial HAC", "_", "Dist=`d' Year=`l'")
        lincomest `eListAdd'
        outreg2 using "`tableName'.xls", br noni dec(3) asterisk(se) nocons nor2 ctitle("`region'","`oVar'") 
      
        //country cluster
        local clust "cID"
        qui areg `oVar' `eList' _1cIDX* if enso_region=="`region'", abs(cID) cluster(`clust')
        unique cID if e(sample)
        local cNum=r(sum)
        qui sum year if e(sample)
        local yMin=r(min)
        local yMax=r(max)
        outreg2 using "`tableName'.xls",br noni dec(3) asterisk(se) nocons nor2  keep(`eList') ctitle("`oVar'") /// 
        addtext("Number of countries", "`cNum'", "Period", "`yMin'-`yMax'", ///
        "Standard errors", "Clustered", "_", "country")
        lincomest `eListAdd'
        outreg2 using "`tableName'.xls", br noni dec(3) asterisk(se) nocons nor2  ctitle("`region'","`oVar'") 
     
        //year cluster
        local clust "year"
        qui areg `oVar' `eList' _1cIDX* if enso_region=="`region'", abs(cID) cluster(`clust')
        unique cID if e(sample)
        local cNum=r(sum)
        qui sum year if e(sample)
        local yMin=r(min)
        local yMax=r(max)
        outreg2 using "`tableName'.xls", br noni dec(3) asterisk(se) nocons nor2  keep(`eList') ctitle("`oVar'") /// 
        addtext("Number of countries", "`cNum'", "Period", "`yMin'-`yMax'", ///
        "Standard errors", "Clustered", "_", "year")
        lincomest `eListAdd'
        outreg2 using "`tableName'.xls", br noni dec(3) asterisk(se) nocons nor2  ctitle("`region'","`oVar'")           
    } 			
}
capture rm "`tableName'.txt"


/****************************** Online Appendix Table 4: nonlinear effect ********************************/
//load data
use Hsiang_Meng_AER_PP_repData, clear
xtset cID year

local tableName "table_nonlinear"
capture rm "`tableName'.xls"
capture rm "`tableName'.txt"

//Creating ENSO bins
local eMin=-1.25
local eMax=1.25
local eInt=.5
local eVar "nino34_late"

egen ninoCat=cut(`eVar'), at(`eMin'(`eInt')`eMax')
replace ninoCat=`eMin' if `eVar'<=`eMin'
replace ninoCat=`eMax' if `eVar'>=`eMax'
egen ninoCatD=group(ninoCat)
xi, pref(_) noomit i.ninoCatD

local eVar "L_nino34_late"
egen LninoCat=cut(`eVar'), at(`eMin'(`eInt')`eMax')
replace LninoCat=`eMin' if `eVar'<=`eMin'
replace LninoCat=`eMax' if `eVar'>=`eMax'
egen LninoCatD=group(LninoCat)
xi, pref(_L) noomit i.LninoCatD


local d=2000
local l=5
local eList "_ninoCatD_1 _ninoCatD_2 _ninoCatD_4 _ninoCatD_5 _ninoCatD_6 _LLninoCatD_1 _LLninoCatD_2 _LLninoCatD_4 _LLninoCatD_5 _LLninoCatD_6"

local rList "TE WA"
foreach region in `rList'{
    
    local oList "temp_annual_crop precip_annual_crop l_yield l_prod l_agVA"
    foreach oVar in `oList' {
    
        qui ols_spatial_HAC  `oVar' `eList' _1cIDX* _1cID_* if enso_region=="`region'", ///
        lat(y_c) lon(x_c) t(year) p(cID) dist(`d') lag(`l') bartlett disp dropvar
        est save M, replace
        unique cID if e(sample)
        local cNum=r(sum)
        qui sum year if e(sample)
        local yMin=r(min)
        local yMax=r(max)
        outreg2 using "`tableName'.xls", br noni dec(3) asterisk(se) nocons nor2 keep(`eList') ctitle("`region'","`oVar'")  /// 
        addtext("Number of countries", "`cNum'", "Period", "`yMin'-`yMax'")
        
        local bList "1 2 4 5 6"
        foreach bin in `bList'{
            est use M
            lincomest _ninoCatD_`bin' + _LLninoCatD_`bin'
            outreg2 using "`tableName'.xls", br noni dec(3) asterisk(se) nocons nor2 ctitle("`region'","`oVar'", `bin')           
        }
    }
} 
   capture rm "`tableName'.txt"
	capture rm M.ster 



 



